package com.mengxingyun.shortlink.admin.toolkit;

import com.alibaba.excel.EasyExcel;
import com.mengxingyun.shortlink.admin.remote.dto.resp.ShortLinkBaseInfoRespDTO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Description: 封装EasyExcel操作Web工具
 * @Author: heshen-mengxingyun
 * @Date: 2024/8/8 12:08
 */
public class EasyExcelWebUtil {
    @SneakyThrows
    public static void write(HttpServletResponse response, String fileName, Class<?> clazz, List<ShortLinkBaseInfoRespDTO> data) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), clazz).sheet("Sheet").doWrite(data);
    }
}
